<?php

/**
 * 微信网页授权
 * @author guoxiaoyong
 */
namespace bc\wx;


use yii;

class OAuth
{
	/**
	 * 不弹出授权页面，直接跳转，只能获取用户openid
	 * 
	 * @var string
	 */
	const SNSAPI_BASE = 'snsapi_base';
	
	/**
	 *
	 * @var string 弹出授权页面，可通过openid拿到昵称、性别、所在地。并且，即使在未关注的情况下，只要用户授权，也能获取其信息
	 */
	const SNSAPI_USERINFO = 'snsapi_userinfo';
	
	/**
	 *
	 * @param string $redirectUri
	 *        	需要跳转的url
	 * @param string $scope
	 *        	| SNSAPI_USERINFO
	 * @return void
	 */
	public function authorize($redirectUri, $scope)
	{
		$wechat = new Wechat();
		$params ['appid'] = Yii::$app->wechat->appid;
		$params ['redirect_uri'] =  urlencode($redirectUri);
		$params ['response_type'] = 'code';
		$params ['scope'] = $scope;
		$params ['state'] = 'zsmm&#wechat_redirect';
		$url = 'https://open.weixin.qq.com' . AppConst::WX_AUTHORIZE;
		
		foreach ( $params as $key => $val )
		{
			$str_params .= $key . '=' . $val . '&';
		}
			
		$url .= '?' . rtrim ( $str_params, '&' );
		
		header('Location:'.$url);
		exit;
	}
	
}
